const strategies = {
  //正整数
  isPositiveNumber: function (value, errorMsg) {
    let reg = /^\+?[1-9][0-9]*$/;
    if (!reg.test(value)) {
      return errorMsg;
    }
  },
  isNotGreaterThan: function (value, maxValue, errorMsg) {
    if (Number(value) > Number(maxValue)) {
      return errorMsg;
    }
  },
  isNumber: function (value, errorMsg) {
    let reg = /^\d+$/;
    if (!reg.test(value)) {
      return errorMsg;
    }
  },
  isNotEmpty: function (value, errorMsg) {
    if (!value) {
      return errorMsg;
    }
  },
  userName: function (name, idType = '1') {
    if (!name) {
      return '请输入真实姓名';
    }
    var msg = '姓名有误，请输入真实姓名';

    name = name.trim();

    if (/\\s+/.test(name)) return msg;

    name = name.replace(/\\s+/g, '');
    var textLength = 150;
    var textLengthCH = textLength / 2;

    var reg_cn = /^[\u4e00-\u9fa5]+(·?[\u4e00-\u9fa5])*[\u4e00-\u9fa5]*$/;
    var reg_en = /^[a-zA-Z]+([/|\s]?[a-zA-z])*[a-zA-Z]*$/;

    if (idType === '1') {
      let arr = ['不详', '不祥', '未知', '不知道'];
      // 身份证
      if (name.length >= 2 && name.length <= textLengthCH && reg_cn.test(name) && arr.indexOf(name) === -1) {
        msg = '';
      }
    } else {
      // 其他证件
      if (reg_cn.test(name) && name.length >= 2 && name.length <= textLengthCH) {
        // 全中文 且长度在2到15(修改后70)之间
        msg = '';
      } else if (reg_en.test(name) && name.length >= 4 && name.length <= textLength) {
        // 全英文 且长度在4到38(修改后140)之间
        msg = '';
      }
    }
    return msg;
  },
  userNameShen: function (name, idType = '1') {
    if (!name) {
      return '请输入真实姓名';
    }
    var msg = '中文姓名只能由汉字,"·"英文姓名只能由半角英文字母和空格,".","/"组成';
    name = name.trim();
    var textLength = 150;
    var textLengthCH = textLength / 2;
    var reg_cn = /^[\u3400-\u9fff]+(·?[\u3400-\u9fff])*[\u3400-\u9fff]*$/;
    var reg_en = /^[a-zA-Z]+([/|\s]?[a-zA-z])*[a-zA-Z]*$/;
    var regFirLa = /^·+|·+$/;
    var regdot = /^[\u3400-\u9fff\w\d\-\_]+\.|,[\u3400-\u9fff\w\d\-\_]+$/;
    var reg_space = /^((?! {2,}).)+$/
    if (idType === '111') {
      msg = ''
    } else {
      let arr = ['不详', '不祥', '未知', '不知道', '先生', '女士', '小姐', '经理', '主任', '老师', '会计', '财务', '联系人', '股份有限公司', '有限责任公司', '有限公司', '中介公司', '经纪代理', '平安养老', '旅行社', '客户'];
      let arr1 = ['abcd', 'abcde', 'ABCD', 'ABCDE'];
      if (name.length >= 2 && name.length <= textLengthCH && reg_cn.test(name) && arr.indexOf(name) !== -1) {
        msg = '您输入的姓名中存在不真实信息，请确认后重新输入';
      } else if (!reg_space.test(name)) {
        msg = '姓名中间不能连续有多个空格';
      } else if ((name.length < 2 || name.length > 25) && reg_cn.test(name)) {
        msg = '您输入的中文姓名长度须为2~25个汉字，请确认后重新输入'
      } else if (reg_cn.test(name) && name.length >= 2 && name.length <= textLengthCH) {
        // 全中文 且长度在2到15(修改后75)之间
        msg = '';
      } else if (regFirLa.test(name)) {
        msg = '您输入的姓名首尾存在“·”，请重新输入'
      } else if (reg_en.test(name) && /[a-z]/.test(name)) {
        msg = '英文姓名有误，英文名字必须是由大写字母以及空格和.符号组成';
      } else if ((name.length < 4 || name.length > 100) && reg_en.test(name)) {
        msg = '英文姓名有误，英文姓名长度须介于4~100个字母';
      } else if (reg_en.test(name) && name.length >= 2 && name.length <= textLength && arr1.indexOf(name) !== -1) {
        // 全英文 且长度在4到38(修改后150)之间
        msg = '您输入的姓名中存在不真实信息，请确认后重新输入';
      } else if (reg_en.test(name) && name.length >= 2 && name.length <= textLength) {
        // 全英文 且长度在4到38(修改后150)之间
        msg = '';
      } else if (regdot.test(name)) {
        msg = '';
      }
    }
    return msg;
  }
}

  const Validator = class {
    constructor() {
      this.cache = [];
    }
    add(value, rules) {
      var self = this;
      for (var i = 0; i < rules.length; i++) {
        var rule = rules[i];
        (function (rule) {
          var strategyAry = rule.strategy.split(':');
          var errorMsg = rule.errorMsg;
          self.cache.push(function () {
            var strategy = strategyAry.shift();
            strategyAry.unshift(value);
            strategyAry.push(errorMsg);
            return strategies[strategy].apply(this, strategyAry);
          });
        })(rule);
      }
    }
    start() {
      for (var i = 0; i < this.cache.length; i++) {
        var validatorFunc = this.cache[i];
        var errorMsg = validatorFunc();
        if (errorMsg) {
          return errorMsg;
        }
      }
    }
  };

  export default Validator;
